package com.imooc.controller;

import com.imooc.base.BaseInfoProperties;
import com.imooc.bo.SearchReportJobBO;
import com.imooc.enums.DealStatus;
import com.imooc.mo.ReportMO;
import com.imooc.result.GraceJSONResult;
import com.imooc.result.ResponseStatusEnum;
import com.imooc.service.ReportService;
import com.imooc.utils.LocalDateUtils;
import com.imooc.utils.PagedGridResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * @author liansen
 * @create 05-29-2:22
 */
@RestController
@RequestMapping("/report")
public class ReportController extends BaseInfoProperties {

    @Autowired
    private ReportService reportService;

    /**
     * 普通用户举报职位   将举报的职位信息存进mongodb中
     * @return
     */
    @PostMapping("/create")
    public GraceJSONResult create(@RequestBody @Valid ReportMO reportMO) {

        boolean existReportJob = reportService.isExistReportJob(reportMO.getReportUserId(), reportMO.getJobId());
        if(existReportJob){
            //请不要重复举报
            return GraceJSONResult.errorCustom(ResponseStatusEnum.REPORT_RECORD_EXIST_ERROR);
        }
        reportService.saveReportJob(reportMO);
        return GraceJSONResult.ok();
    }


    /**
     * 运维端查询举报的职位列表 分页
     */
    @PostMapping("/pagedReportRecordList")
    public GraceJSONResult pagedReportRecordList(@RequestBody SearchReportJobBO searchReportJobBO, Integer page, Integer limit){

        //mongodb从0分页, 区别于数据库
        if(page == null){
            page = COMMON_START_PAGE_ZERO;
        }
        if(limit == null){
            limit = COMMON_PAGE_SIZE;
        }

        LocalDate beginDate = searchReportJobBO.getBeginDate();
        LocalDate endDate = searchReportJobBO.getEndDate();

        if(beginDate != null){
            String beginDateTimeStr = LocalDateUtils.format(beginDate, LocalDateUtils.DATE_PATTERN) + " 00:00:00";
            LocalDateTime beginDateTime = LocalDateUtils.parseLocalDateTime(beginDateTimeStr, LocalDateUtils.DATETIME_PATTERN);
            searchReportJobBO.setBeginDateTime(beginDateTime);
        }

        if(endDate != null){
            String endDateTimeStr = LocalDateUtils.format(endDate, LocalDateUtils.DATE_PATTERN) + " 23:59:59";
            LocalDateTime endDateTime = LocalDateUtils.parseLocalDateTime(endDateTimeStr, LocalDateUtils.DATETIME_PATTERN);
            searchReportJobBO.setEndDateTime(endDateTime);
        }

        PagedGridResult result = reportService.pagedReportRecordList(searchReportJobBO, page, limit);

        return GraceJSONResult.ok(result);
    }

    /**
     * 运维端删除被举报的职位
     */
    @PostMapping("/deal/delete")
    public GraceJSONResult dealDelete(String reportId){
        //mongo中被举报的状态改为已处理
        //mysql中的职位状态改为删除
        reportService.updateReportRecordStatus(reportId, DealStatus.DONE);
        return GraceJSONResult.ok();
    }

    /**
     * 运维端忽略被举报的职位
     */
    @PostMapping("/deal/ignore")
    public GraceJSONResult dealIgnore(String reportId){
        reportService.updateReportRecordStatus(reportId, DealStatus.IGNORE);
        return GraceJSONResult.ok();
    }
}


























